package com.base.pig.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.base.pig.entity.CameraInfo;
import com.base.pig.entity.CameraLecheng;
import com.base.pig.entity.DeviceConfig;
import com.base.pig.service.CameraLechengService;
import com.base.pig.util.LechangeHttpSend;
import com.base.pig.util.NlLechangeHttpSend;
import com.base.pig.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(tags = "乐橙云接口")
@RestController
@RequestMapping("/lcyApi")
public class LcyApiController {

    @Autowired
    public CameraLechengService cameraLechengService;

    @ApiOperation("查询摄像头信息列表")
    @GetMapping("/queryCameraList")
    public R queryCameraList(){
        List<CameraLecheng> list = cameraLechengService.list();
        return R.ok(list);
    }



    @ApiOperation("根据设备deviceid查询摄像头信息")
    @GetMapping("/queryCameraInfo")
    public R queryCameraInfo(@RequestParam("deviceId") String deviceId){
        CameraLecheng cameraInfo = cameraLechengService.getOne(Wrappers.<CameraLecheng>query().lambda().eq(CameraLecheng::getDeviceid,deviceId));
        return R.ok(cameraInfo);
    }

    @ApiOperation("查询设备本地录像片段")
    @GetMapping("/queryLocalRecords")
    public R queryLocalRecords(@RequestParam("deviceId") String deviceId,
                               @RequestParam("beginTime") String beginTime,
                               @RequestParam("endTime") String endTime){
        CameraLecheng camera = cameraLechengService.getOne(Wrappers.<CameraLecheng>query().lambda().eq(CameraLecheng::getDeviceid,deviceId));
        String accessToken = NlLechangeHttpSend.getAccessToken();
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("token",accessToken);
        paramMap.put("deviceId",camera.getCamerakey());
        paramMap.put("channelId",camera.getCamerachanel());
        paramMap.put("beginTime",beginTime);
        paramMap.put("endTime",endTime);
        return R.ok(NlLechangeHttpSend.queryLocalRecords(paramMap));
    }

    @ApiOperation("云台移动控制")
    @GetMapping("/controlMovePTZ")
    public R controlMovePTZ(@RequestParam("deviceId") String deviceId,
                            @RequestParam("operation") String operation,
                            @RequestParam("duration") Long duration){
        CameraLecheng camera = cameraLechengService.getOne(Wrappers.<CameraLecheng>query().lambda().eq(CameraLecheng::getDeviceid,deviceId));
        String accessToken = NlLechangeHttpSend.getAccessToken();
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("token",accessToken);
        paramMap.put("deviceId",camera.getCamerakey());
        paramMap.put("channelId",camera.getCamerachanel());
        //0-上，1-下，2-左，3-右，4-左上，5-左下，6-右上，7-右下，8-放大，9-缩小，10-停止
        paramMap.put("operation",operation);
        paramMap.put("duration",duration);

        return R.ok(NlLechangeHttpSend.controlMovePTZ(paramMap));
    }

    @ApiOperation("获取KitToken")
    @GetMapping("/getKitToken")
    public R getKitToken(@RequestParam("deviceId") String deviceId){
        CameraLecheng camera = cameraLechengService.getOne(Wrappers.<CameraLecheng>query().lambda().eq(CameraLecheng::getDeviceid,deviceId));
        String accessToken = NlLechangeHttpSend.getAccessToken();
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("token",accessToken);
        paramMap.put("deviceId",camera.getCamerakey());
        paramMap.put("channelId",camera.getCamerachanel());
        return R.ok(NlLechangeHttpSend.getKitToken(paramMap));
    }

}
